<?php
if (!defined('INDEX') || INDEX == false) die ('Acc�s denied');
include_once COREDIR . 'core_exception.php';

/**
 * Core loader
 *
 * @version 0.1
 * @copyright 2010
 */

/**
 * Core loader
 *
 */
class core_loader
{
	/**
	 * Constructor
	 */
	function __construct()
	{
		try
		{
			$this->InitDatabase(DataConfig::$DBDriver, DataConfig::$DBControler);
			core_settings::LoadStettings();
			hash::SetKey(DataConfig::$CryptoKey);
			core_modules::LoadModules();
			$this->LoadTheme();
			$this->initGz();
		}
		catch (CoreException $e)
		{
			$this->displayError($e);
			exit;
		}
	}
	
	public function initGz()
	{
		if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)
		{
			$gz = core_settings::Get('gzoutput');
			if ($gz == 'true')
			{
				ob_start('ob_gzhandler');
				header('Content-Encoding: gzip');
			}
		}
	}
	
	public function LoadTheme()
	{
		$dir = ROOTDIR . core_settings::Get('themedir') . DIRECTORY_SEPARATOR;
		core_modules::LoadUnregistredMod($dir . core_settings::Get('theme') . DIRECTORY_SEPARATOR . 'mod_theme.php');		
	}

	public function InitDatabase($Driver, $Controler)
	{
		$Driver = LIBSDIR . $Driver . DIRECTORY_SEPARATOR . 'sqld.php';
		$Controler = LIBSDIR . $Controler . DIRECTORY_SEPARATOR . 'sqlc.php';

		if (!file_exists($Driver)
		or !file_exists($Controler))
		{
			throw new Exception('Can\'t load the database driver. Check your configuration. (file ' . $Driver . ' & ' . $Controler . ')');
		}
		else
		{
			include $Driver;
			include $Controler;
			sqld::connect(DataConfig::$DBUser, DataConfig::$DBPassword, DataConfig::$DBServer, DataConfig::$DBPort, DataConfig::$DBase);
			DataConfig::$DBUser = DataConfig::$DBPassword = DataConfig::$DBServer = DataConfig::$DBPort = DataConfig::$DBase = null;

			if (!class_exists('sql'))
			{
				throw new Exception('We can\'t use the database driver.');
			}
		}
	}
	
	private function UsedMod ($url)
	{
		$mod = core_modules::IterYes(array($url));
		if (count($mod) > 0)
		{
			return $mod[0];
		}
		else
		{
			$url = explode('/', $url);
			if (isset($url[0]))
			{
				$mod = core_modules::FindClass(strtolower($url[0]));
				if ($mod !== false)
					return $mod;
			}
			return core_settings::Get('def_mod');
		}
	}
	
	private function GetUrl ()
	{
		if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '')
		{
			return substr($_SERVER['PATH_INFO'],1);
		}
		elseif (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '')
		{
			return $_SERVER['QUERY_STRING'];
		}
		else
		{
			return '';
		}
	}
	
	public function start ()
	{
		$url = $this->GetUrl();
		$mod = $this->UsedMod($url);
		$mod_array = explode('/', $url);
		
		try
		{
			/* Lib Exec */
			if ($mod_array[0] == 'libexec' && method_exists($mod_array[1], 'libexec'))
			{
				call_user_func(array($mod_array[1],'libexec'), $url);
			}
			elseif ($mod !== false)
			{
				$mod = core_modules::GetInstance($mod);
				$mod->Start($url);
			}
			else
				throw new CoreException('Default mod is not found');
		}
		catch (CoreException $err)
		{
			$this->displayError ($err);
		}
	}
	
	private function displayError ($err)
	{
		$page = file_get_contents(COREDIR . 'error_tpl.html');
		foreach ($err->getTplVar() as $var => $value)
			$page = preg_replace('`{'.$var.'}`i', $value, $page);
		echo $page;
	}
}
?>
